package com.example.as_sum.util;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.json.JSONObject;

/**
 * Created by ran on 2016/7/15.
 */
public class RequestUtil {
    public static void get(){
    }

    /**
     * 发送post请求
     * @param mHandler  handler来传递网络返回的数据
     * @param url  接口地址
     * @param param  接口需要的参数
     * @param type   区别是哪个请求，第一个请求时what=1，第二个是what=2
     */
    public static void post(Handler mHandler,String url,String param,int... type){
        //发送网络请求，返回数据
        boolean isSucess=true;
        //isSucess=true请求成功，false请求失败
        if(isSucess) {
            try {
                Message msg = Message.obtain();
                msg.what = type[0];
                JSONObject object = new JSONObject("返回的内容");
                String data = object.getString("data");
                JSONObject jsonObject = new JSONObject(data);
                msg.obj = jsonObject;
                mHandler.sendMessage(msg);
            } catch (Exception e) {
                Log.e("", e.toString());
            }
        }else{
            try {
                JSONObject object = new JSONObject("返回的内容");
                Bundle bundle = new Bundle();
                Message msg = Message.obtain();

                //错误信息保存进去，通过Handler来传递
                bundle.putCharSequence("error",
                        object.getString("msg"));

                msg.setData(bundle);
                msg.arg1 = type[0];
                msg.what = 0;//what=0表示请求业务出错了，请求还是成功的
                //错误状态码，arg2里面保存就是错误的状态码
                String m = object.getString("resCode");
                msg.arg2 = Integer.parseInt(m);
                mHandler.sendMessage(msg);
            }catch (Exception e) {
                Log.e("", e.toString());
            }
        }
    }
}
